package com.css.bjdt_core.reportDataContrast.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.reportDataContrast.entity.ReportDataContrast;

@Service
public class ReportDataContrastService {
	@Autowired
	private Dao dao;

	public List<ReportDataContrast> getLineName(String dzzId) {
		String sqlStr = "select LINE_NAME from RPT_DATA_CONTRAST where DZZ_ID = @dzzId group by LINE_NAME";
		Sql sql = Sqls.create(sqlStr);
		sql.params().set("dzzId", dzzId);
		sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<ReportDataContrast> reportDataContrastList = new ArrayList<ReportDataContrast>();
				while (rs.next()) {
					ReportDataContrast reportDataContrast = new ReportDataContrast();
					reportDataContrast.setLineName(rs.getString("LINE_NAME"));
					reportDataContrastList.add(reportDataContrast);
				}
				return reportDataContrastList;
			}
		});
		dao.execute(sql);
		return sql.getList(ReportDataContrast.class);
	}

	public List<ReportDataContrast> getAllReportDataContrastByDzzId(String dzzId) {
		return dao.query(ReportDataContrast.class, Cnd.where("DZZ_ID", "=", dzzId));
	}

	public void deleteAllReportDataContrastByDzzId(String dzzId) {
		dao.clear(ReportDataContrast.class, Cnd.where("DZZ_ID", "=", dzzId));
	}

	public void insertAllReportDataContrastByDzzId(List<ReportDataContrast> reportDataContrastList) {
		dao.insert(reportDataContrastList);
	}

	public List<ReportDataContrast> getIds(String dzzId) {
		String sqlStr = "select ID from RPT_DATA_CONTRAST where DZZ_ID = @dzzId group by ID";
		Sql sql = Sqls.create(sqlStr);
		sql.params().set("dzzId", dzzId);
		sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<ReportDataContrast> reportDataContrastList = new ArrayList<ReportDataContrast>();
				while (rs.next()) {
					ReportDataContrast reportDataContrast = new ReportDataContrast();
					reportDataContrast.setId(rs.getString("ID"));
					reportDataContrastList.add(reportDataContrast);
				}
				return reportDataContrastList;
			}
		});
		dao.execute(sql);
		return sql.getList(ReportDataContrast.class);
	}

	public List<ReportDataContrast> getAnnualName(String dzzId) {
		String sqlStr = "select ANNUAL_NAME from RPT_DATA_CONTRAST where DZZ_ID = @dzzId group by ANNUAL_NAME";
		Sql sql = Sqls.create(sqlStr);
		sql.params().set("dzzId", dzzId);
		sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<ReportDataContrast> reportDataContrastList = new ArrayList<ReportDataContrast>();
				while (rs.next()) {
					ReportDataContrast reportDataContrast = new ReportDataContrast();
					reportDataContrast.setAnnualName(rs.getString("ANNUAL_NAME"));
					reportDataContrastList.add(reportDataContrast);
				}
				return reportDataContrastList;
			}
		});
		dao.execute(sql);
		return sql.getList(ReportDataContrast.class);
	}

	public List<ReportDataContrast> getAllReportDataContrastById(String id) {
		return dao.query(ReportDataContrast.class, Cnd.where("ID", "=", id));
	}
}
